package com.anolesoft.amljmes.billing.controller;

import com.alibaba.fastjson.JSONObject;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.amljmes.billing.dto.WorkshopInfoDto;
import com.anolesoft.amljmes.billing.service.WorkshopInfoService;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/jcwh/workshop")
public class WorkshopInfoController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper = true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String workshopName;
        private String factoryId;
    }

    @Autowired
    public WorkshopInfoService workshopInfoService;

    //查询
    @PostMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryWorkshopInfo(DemoVO demoVO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<WorkshopInfoDto> allList = workshopInfoService.queryWorkshopInfo(demoVO);
        int pn = demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn * ps;
        List<WorkshopInfoDto> list = allList.subList((pn - 1) * ps, endIndex);
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageResult);
    }

    //新增
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addWorkshop(@RequestBody WorkshopInfoDto queryInfoDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, workshopInfoService.addWorkshop(queryInfoDto));
    }

    //修改
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateWorkshop(@RequestBody WorkshopInfoDto queryInfoDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setXgr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, workshopInfoService.updateWorkshop(queryInfoDto));
    }

    //删除
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult deleteWorkshop(@RequestBody WorkshopInfoDto queryInfoDto) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, workshopInfoService.deleteWorkshop(queryInfoDto));
    }

    // 验证名称重复
    @PostMapping(value = "/workshopNameRules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isWorkshopName(@RequestBody WorkshopInfoDto queryInfoDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());

        String workshopName = "";
        int count = workshopInfoService.isWorkshopName(queryInfoDto);
        if (count > 0) {
            workshopName = "名称已存在！";
        }
        return new AjaxResult(HttpStatus.SUCCESS, workshopName);
    }

    //动态下拉
    @PostMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult workshopAll() {
//        List<WorkshopInfoDto> list = workshopInfoService.workshopAll();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DemoVO demoVO = DemoVO.builder().build();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<WorkshopInfoDto> allList = workshopInfoService.queryWorkshopInfo(demoVO);
        AjaxResult success = AjaxResult.success();
        success.put("data", allList);
        return success;
    }
}
